/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef PLUGINURLCONTAINER_H
#define PLUGINURLCONTAINER_H

#include "interfaceUrlContainer.h"
#include "exceptions.h"

#include <QObject>
#include <QPluginLoader>
#include <QDebug>
#include <QFile>
#include <QPointer>
#include <QRegExp>
#include <QDir>
#include <QUrl>
#include <QNetworkProxy>

class PluginUrlContainer : public QObject
{
    Q_OBJECT

    public:

        enum E_ERRORS
        {
            ERROR_PLUGIN
        };

        struct S_PLUGIN
        {
            QString pluginFile;

            QPluginLoader *pluginLoader;
            InterfaceUrlContainer *urlContainerInterface;
            QObject *urlContainerObject;

            S_PLUGIN()
            {
                pluginFile = "";
                pluginLoader = 0;
                urlContainerInterface = 0;
                urlContainerObject = 0;
            }
        };

        PluginUrlContainer();
        ~PluginUrlContainer();

        void start();
        void abort();

        void submitCaptcha(QString const& captcha);

        void loadPlugins(QDir pluginDir);

        bool isUrlContainer(QString const& link);
        bool isUrlContainer(QUrl const& url);
        bool selectPluginForLink(QString const& link);
        bool selectPluginForLink(QUrl const& url);

        void setCaptchaDir(QString const& path) { captchaDir = path; };
        void setProxy(QNetworkProxy const& proxy);

    signals:

        void captcha(QString const& captcha);
        void finished(QStringList const& links, bool const& error);
        void progressChanged(int const& progress);
        void stateChanged();

        void errorMsg(QString const& error);

    private:

        QList<S_PLUGIN*> plugins;

        int activePlugin;
        QUrl activeUrl;

        QString captchaDir;
        QNetworkProxy _proxy;

    private slots:

        void slot_plugin_captcha(QString const& _captcha);
        void slot_plugin_finished(QStringList const& links, bool const& error);        
        void slot_plugin_progressChanged(int const& progress);
        void slot_plugin_stateChanged();
};

#endif // PLUGINURLCONTAINER_H
